package com.jinliang.blogweb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinliang.blogweb.entity.Article;
import com.jinliang.blogweb.mapper.ArticleMapper;
import com.jinliang.blogweb.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "markdown")
public class ArticleController {
    private final ArticleMapper articleMapper;
    @Autowired
    ArticleService articleService;

    public ArticleController(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }

    @RequestMapping(value = "save")
    public Boolean save(@RequestBody Article article) {
        articleService.add(article);
        System.out.println(article.toString());
        return Boolean.TRUE;
    }

    @GetMapping("/get")
    public Object getObjByParam(@RequestParam("id") String id) {
        Article article1 = articleMapper.selectById(id);
        return article1;
    }

    @RequestMapping("/getList")
    public List<Article> getObjByParamList() {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        List<Article> articles = articleMapper.selectByMap(objectObjectHashMap);
        return articles;
    }

    @RequestMapping("/getListPage")
    public IPage<Article> getListPage(@RequestBody Page page) {
        page = articleMapper.selectPage(page.setSize(5), null);
        return page;
    }

}
